{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# Quick Start Example - Extracting a concept from a document"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Quick Start Example - Extracting a concept from a document\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Document, DocumentLLM, JsonObjectConcept\n",
        "\n",
        "\n",
        "# Example document instance\n",
        "# Document content is shortened for brevity\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"Statement of Work\\n\"\n",
        "        \"Project: Cloud Migration Initiative\\n\"\n",
        "        \"Client: Acme Corporation\\n\"\n",
        "        \"Contractor: TechSolutions Inc.\\n\\n\"\n",
        "        \"Project Timeline:\\n\"\n",
        "        \"Start Date: March 1, 2025\\n\"\n",
        "        \"End Date: August 31, 2025\\n\\n\"\n",
        "        \"Deliverables:\\n\"\n",
        "        \"1. Infrastructure assessment report (Due: March 15, 2025)\\n\"\n",
        "        \"2. Migration strategy document (Due: April 10, 2025)\\n\"\n",
        "        \"3. Test environment setup (Due: May 20, 2025)\\n\"\n",
        "        \"4. Production migration (Due: July 15, 2025)\\n\"\n",
        "        \"5. Post-migration support (Due: August 31, 2025)\\n\\n\"\n",
        "        \"Budget: $250,000\\n\"\n",
        "        \"Payment Schedule: 20% upfront, 30% at midpoint, 50% upon completion\\n\"\n",
        "    ),\n",
        ")\n",
        "\n",
        "# Define a document-level concept using e.g. JsonObjectConcept\n",
        "# This will extract structured data from the entire document\n",
        "doc_concept = JsonObjectConcept(\n",
        "    name=\"Project Details\",\n",
        "    description=\"Key project information including timeline, deliverables, and budget\",\n",
        "    structure={\n",
        "        \"project_name\": str,\n",
        "        \"client\": str,\n",
        "        \"contractor\": str,\n",
        "        \"budget\": str,\n",
        "        \"payment_terms\": str,\n",
        "    },  # simply use a dictionary with type hints (including generic aliases and union types)\n",
        "    add_references=True,\n",
        "    reference_depth=\"paragraphs\",\n",
        ")\n",
        "\n",
        "# Add the concept to the document\n",
        "doc.add_concepts([doc_concept])\n",
        "# (add more concepts to the document, if needed)\n",
        "\n",
        "# Create an LLM for extraction\n",
        "llm = DocumentLLM(\n",
        "    model=\"openai/gpt-4o-mini\",  # or any other LLM from e.g. Anthropic, etc.\n",
        "    api_key=os.environ.get(\"CONTEXTGEM_OPENAI_API_KEY\"),  # your API key\n",
        ")\n",
        "\n",
        "# Extract information from the document\n",
        "extracted_concepts = llm.extract_concepts_from_document(doc)\n",
        "# or use async version llm.extract_concepts_from_document_async(doc)\n",
        "\n",
        "# Access extracted information\n",
        "print(\"Project Details:\")\n",
        "print(\n",
        "    extracted_concepts[0].extracted_items\n",
        ")  # extracted concept items with references to paragraphs\n",
        "# Or doc.get_concept_by_name(\"Project Details\").extracted_items\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}